home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / RMI_OS / RMI-PREB / EXAMPLES / STOCK / STOCKSER.JAV < prev    next >
Encoding:
Text File  |  1996-11-08  |  6.6 KB  |  216 lines

  1. /*
  2.  * %W% %E%
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_pre-beta
  20.  */
  21. package examples.stock;
  22.  
  23. import java.rmi.*;
  24. import java.rmi.server.*;
  25. import java.rmi.registry.LocateRegistry;
  26. import java.util.*;
  27.  
  28. public class StockServer extends UnicastRemoteObject
  29.     implements StockWatch, Runnable
  30. {
  31.     /** table that maps StockNotify objects to a vector of stocks */
  32.     private Hashtable notifyTable = new Hashtable();
  33.     /** table that maps stock names to stock update info */
  34.     private Hashtable stockTable = new Hashtable();
  35.     /** thread for notifying watchers of stock updates */
  36.     private Thread notifier = null;
  37.  
  38.     private static String name[] = { "Sun", "HP", "Microsoft", "DEC","Novell",
  39.                      "IBM","Apple","Netscape","Borland","SGI"};
  40.     /**
  41.      * Construct the stock server
  42.      * @exception RemoteException if remote object cannot be exported
  43.      */
  44.     public StockServer() throws RemoteException 
  45.     {
  46.     for (int i=0; i<name.length; i++) {
  47.         stockTable.put(name[i], new Stock(name[i]));
  48.     }
  49.     }
  50.  
  51.     /**
  52.      * Request notification of stock updates.
  53.      * @param stock the stock name
  54.      * @param obj the remote object to be notified
  55.      * @return the latest update of the stock
  56.      * @exception StockNotFoundException if stock is not known
  57.      */
  58.     public synchronized Stock watch (String stock, StockNotify obj)
  59.     throws StockNotFoundException
  60.     {
  61.     System.out.println("StockServer.watch: " + stock );
  62.     
  63.     if (!stockTable.containsKey(stock))
  64.         throw new StockNotFoundException(stock);
  65.     
  66.     Vector stocks = (Vector)notifyTable.get(obj);
  67.  
  68.     // register intereted party...
  69.     if (stocks == null) {
  70.         stocks = new Vector();
  71.         notifyTable.put(obj, stocks);
  72.     }
  73.  
  74.     // add stock to list
  75.     if (!stocks.contains(stock)) {
  76.         stocks.addElement(stock);
  77.     }
  78.     
  79.     // start thread to notify watchers...
  80.     if (notifier == null) {
  81.         notifier = new Thread(this, "StockNotifier");
  82.         notifier.start();
  83.     }
  84.     return (Stock)stockTable.get(stock);
  85.     }
  86.  
  87.     /**
  88.      * Cancel request for stock updates for a particular stock.
  89.      * @param stock the stock name
  90.      * @param obj the remote object canceling the notification
  91.      */
  92.     public void cancel(String stock, StockNotify obj)
  93.     {
  94.     Vector stocks = (Vector)notifyTable.get(obj);
  95.     stocks.removeElement(stock);
  96.     }
  97.  
  98.    /**
  99.      * Returns an array of stock update information for the stocks
  100.      * already registered by the remote object.
  101.      * @param obj the remote object
  102.      * @return the list of stocks, or null if obj is not watching any
  103.      *  stocks
  104.      * @exception RemoteException if some communication failure occurs
  105.      */
  106.     public Stock[] list(StockNotify obj)
  107.     {
  108.     Vector stocks = (Vector)notifyTable.get(obj);
  109.     Stock[] stockList = null;
  110.     
  111.     if (stocks != null) {
  112.         Enumeration enum = stocks.elements();
  113.         stockList = new Stock[stocks.size()];
  114.         int i=0;
  115.         // collect updates to the stocks this watcher is
  116.         // interested in
  117.         while (enum.hasMoreElements()) {
  118.         String stockname = (String)enum.nextElement();
  119.         stockList[i++] = (Stock)stockTable.get(stockname);
  120.         }
  121.     }
  122.     return stockList;
  123.     }
  124.  
  125.     /**
  126.      * Cancel all requests for stock updates for the remote object.
  127.      * @param obj the remote object canceling the request
  128.      * @exception RemoteException if some communication failure occurs
  129.      */
  130.     public synchronized void cancelAll(StockNotify obj)
  131.     {
  132.     notifyTable.remove(obj);
  133.     if (notifyTable.isEmpty()) {
  134.         Thread thread = notifier; // in case current thread is notifier
  135.         notifier = null;
  136.         thread.stop();
  137.     }
  138.     }
  139.  
  140.     /**
  141.      * Private method to generate random stock updates
  142.      */
  143.     private void generateUpdates() 
  144.     {
  145.     Enumeration enum = stockTable.elements();
  146.     while (enum.hasMoreElements()) {
  147.         Stock stock = (Stock)enum.nextElement();
  148.         stock.update();
  149.     }
  150.     }
  151.  
  152.     /**
  153.      * The run method (called from the notifier thread) sends out stock
  154.      * updates periodically to those remote objects that have
  155.      * registered interest in being notified.
  156.      */
  157.     public void run() 
  158.     {
  159.     while (true) {
  160.         try {
  161.         // wait for a few seconds between updates
  162.         Thread.currentThread().sleep(2000);
  163.         } catch (InterruptedException e) {
  164.         }
  165.  
  166.         Date date = new Date();
  167.         // update stocks in table
  168.         generateUpdates();
  169.         
  170.         // enumerate through each watcher...
  171.         Enumeration enum = notifyTable.keys();
  172.         while (enum.hasMoreElements()) {
  173.         StockNotify obj = (StockNotify)enum.nextElement();
  174.         Stock[] stockList = list(obj);
  175.         if (stockList != null) {
  176.             // send update
  177.             try {
  178.             System.out.println("StockServer.run: sending update " +
  179.                        date);
  180.             obj.update(date, stockList);
  181.             } catch (RemoteException e) {
  182.             // can't reach watcher; cancel notification request
  183.             System.out.println("StockServer.run: exception");
  184.             e.printStackTrace();
  185.             cancelAll(obj);
  186.             }
  187.         }
  188.         }
  189.     }
  190.     }
  191.  
  192.     /**
  193.      * Start up the stock server; also creates a registry so that the
  194.      * StockApplet can lookup the server.
  195.      */
  196.     public static void main(String args[]) 
  197.     {
  198.     // Create and install the security manager
  199.     System.setSecurityManager(new RMISecurityManager());
  200.  
  201.     try {
  202.         System.out.println("StockServer.main: creating registry");
  203.         LocateRegistry.createRegistry(2005);
  204.         System.out.println("StockServer.main: creating server");
  205.         StockServer server = new StockServer();
  206.         System.out.println("StockServer.main: binding server ");
  207.         Naming.rebind("//:2005/example.stock.StockServer", server);
  208.         System.out.println("StockServer.main: done");
  209.     } catch (Exception e) {
  210.         System.out.println("StockServer.main: an exception occurred: " +
  211.                    e.getMessage());
  212.         e.printStackTrace();
  213.     }
  214.     }
  215. }
  216.